using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using QSBusinessLayerCSharp;
using ValidationFramework.Web.ASP;

namespace QSWebApplicationCSharp
{


	internal delegate Control FindControl(string name); 

	/// <summary>
	/// This helper class only exists because I am updating and validating the same data in several web forms.
	/// </summary>
	internal static class Helper
	{
		internal static Person GetPerson(FindControl findControl)
		{

			var firstNameTextBox = (TextBox)findControl("firstNameTextBox");
			var lastNameTextBox = (TextBox)findControl("lastNameTextBox");
			var emailAddressTextBox = (TextBox)findControl("emailAddressTextBox");
			var creditCardNumberTextBox = (TextBox)findControl("creditCardNumberTextBox");
			var ageTextBox = (TextBox)findControl("ageTextBox");
			var orderTotalTextBox = (TextBox)findControl("orderTotalTextBox");
			var phoneNumberTextBox = (TextBox)findControl("phoneNumberTextBox");
			var cardTypeDropDownList = (DropDownList)findControl("cardTypeDropDownList");
			var person = new Person
			                    {
			                        FirstName = firstNameTextBox.Text,
			                        LastName = lastNameTextBox.Text,
			                        EmailAddress = emailAddressTextBox.Text,
			                        PhoneNumber = phoneNumberTextBox.Text,
			                        CreditCardNumber = creditCardNumberTextBox.Text,
			                        CardType = ((CardType) Enum.Parse(typeof (CardType), cardTypeDropDownList.SelectedValue))
			                    };
		    int age;
			if (int.TryParse(ageTextBox.Text, out age))
			{
				person.Age = age;
			}
			decimal orderTotal;
			object o = orderTotalTextBox.Text;
			if (decimal.TryParse((string)o, out orderTotal))
			{
				person.OrderTotal = orderTotal;
			}
			return person;
		}

        internal static void AddValidation(PropertyValidatorGenerator validatorGenerator, FindControl findControl)
		{
			var firstNameTextBox = (TextBox)findControl("firstNameTextBox");
			var lastNameTextBox = (TextBox)findControl("lastNameTextBox");
			var emailAddressTextBox = (TextBox)findControl("emailAddressTextBox");
			var creditCardNumberTextBox = (TextBox)findControl("creditCardNumberTextBox");
			var ageTextBox = (TextBox)findControl("ageTextBox");
			var orderTotalTextBox = (TextBox)findControl("orderTotalTextBox");
			var phoneNumberTextBox = (TextBox)findControl("phoneNumberTextBox");
			//DropDownList cardTypeDropDownList = (DropDownList) findControl("cardTypeDropDownList");
			validatorGenerator.AddAssociation(firstNameTextBox, Person.FirstNameMember);
			validatorGenerator.AddAssociation(lastNameTextBox, Person.LastNameMember);
			validatorGenerator.AddAssociation(emailAddressTextBox, Person.EmailAddressMember);
			validatorGenerator.AddAssociation(creditCardNumberTextBox, Person.CreditCardNumberMember);
			validatorGenerator.AddAssociation(ageTextBox, Person.AgeMember);
			validatorGenerator.AddAssociation(orderTotalTextBox, Person.OrderTotalMember);
			validatorGenerator.AddAssociation(phoneNumberTextBox, Person.PhoneNumberMember);
			validatorGenerator.GenerateValidators();
		}

	}
}
